package com.ruoyi.common.db;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <pre>
 *
 * </pre>
 *
 * @author zhangli
 * @since 2023/4/7
 */
public abstract class BaseService<KeyType extends Serializable, Po extends ID<KeyType>> {

    @Autowired
    private IService<Po> mapperService;

    public Po getById(KeyType id) {
        return mapperService.getById(id);
    }

    public List<Po> selectAll() {
        QueryWrapper<Po> queryWrapper = new QueryWrapper<>();
        return mapperService.getBaseMapper().selectList(queryWrapper);
    }

    public Po findOne(QueryWrapper<Po> queryWrapper) {
        return mapperService.getBaseMapper().selectOne(queryWrapper);
    }

    public List<Po> selectList(QueryWrapper<Po> queryWrapper) {
        return mapperService.getBaseMapper().selectList(queryWrapper);
    }

    public boolean save(Po po) {
        return mapperService.saveOrUpdate(po);
    }

    public boolean save(List<Po> poList) {
        return mapperService.saveOrUpdateBatch(poList);
    }

    public boolean existsById(KeyType id) {
        return getById(id) != null;
    }

    public boolean removeById(KeyType id) {
        return mapperService.removeById(id);
    }

    public boolean delete(QueryWrapper<Po> queryWrapper){
        return mapperService.remove(queryWrapper);
    }

    /**
     * 翻页查询
     *
     * @param page         翻页对象
     * @param queryWrapper 实体对象封装操作类 {@link com.baomidou.mybatisplus.core.conditions.query.QueryWrapper}
     */
    public <E extends IPage<Po>> E page(E page, Wrapper<Po> queryWrapper) {
        return mapperService.page(page, queryWrapper);
    }

    /**
     * 无条件翻页查询
     *
     * @param page 翻页对象
     * @see Wrappers#emptyWrapper()
     */
    public <E extends IPage<Po>> E page(E page) {
        return mapperService.page(page);
    }

    public IService<Po> getDbService(){
        return mapperService;
    }
}
